package com.jsh.erp.service.sequence;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.constants.BusinessConstants;
import com.jsh.erp.constants.ExceptionConstants;
import com.jsh.erp.datasource.entities.*;
import com.jsh.erp.datasource.mappers.*;
import com.jsh.erp.exception.BusinessRunTimeException;
import com.jsh.erp.exception.JshException;
import com.jsh.erp.service.log.LogService;
import com.jsh.erp.service.user.UserService;
import com.jsh.erp.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Description
 *
 * @Author: jishenghua
 * @Date: 2021/3/16 16:33
 */
@Service
public class SequenceService {
    private Logger logger = LoggerFactory.getLogger(SequenceService.class);

    @Resource
    private SequenceMapperEx sequenceMapperEx;

    @Resource
    private SequenceMapper sequenceMapper;

    public SerialNumber getSequence(long id) throws Exception {
        return null;
    }

    public List<SerialNumberEx> select(String name, Integer offset, Integer rows) throws Exception {
        return null;
    }

    public Long countSequence(String name) throws Exception {
        return null;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int insertSequence(JSONObject obj, HttpServletRequest request) throws Exception {
        return 0;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int updateSequence(JSONObject obj, HttpServletRequest request) throws Exception {
        return 0;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int deleteSequence(Long id, HttpServletRequest request) throws Exception {
        return 0;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int batchDeleteSequence(String ids, HttpServletRequest request) throws Exception {
        return 0;
    }

    public int checkIsNameExist(Long id, String serialNumber) throws Exception {
        return 0;
    }

    /**
     * 获取构建存储的前缀
     *
     * @param prefix
     * @return
     */
    private String getBuildNumberKey(String prefix) {
        prefix = StrUtil.blankToDefault(prefix, BusinessConstants.DEPOT_NUMBER_SEQ);
        return prefix + DateUtil.format(new Date(), "yyyyMMdd");
    }

    private Sequence createSequence(String seqName) {
        Sequence sequence = new Sequence();
        sequence.setSeqName(seqName);
        sequence.setMinValue(0L);
        sequence.setMaxValue(Long.MAX_VALUE);
        sequence.setCurrentVal(0L);
        sequenceMapper.insert(sequence);
        return sequence;
    }

    /**
     * 创建序列号
     *
     * @param prefix
     * @return
     */
    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public String buildOnlyNumber(String prefix) {
        Long buildOnlyNumber = null;
        String key = getBuildNumberKey(prefix);
        synchronized (this) {
            //检查配置是否存在并创建一个默认的
            Long countForSeqName = sequenceMapperEx.getCountForSeqName(key);
            if (countForSeqName == 0) {
                createSequence(key);
            }
            //编号+1
            sequenceMapperEx.updateBuildOnlyNumber(key);
            buildOnlyNumber = sequenceMapperEx.getBuildOnlyNumber(key);
        }
        String buildNumberStr = buildOnlyNumber.toString();
        if (StrUtil.length(buildNumberStr) < BusinessConstants.SEQ_TO_STRING_MIN_LENGTH) {
            StringBuffer sb = new StringBuffer(buildOnlyNumber.toString());
            int len = BusinessConstants.SEQ_TO_STRING_MIN_LENGTH.toString().length() - sb.length();
            for (int i = 0; i < len; i++) {
                sb.insert(0, BusinessConstants.SEQ_TO_STRING_LESS_INSERT);
            }
            buildNumberStr = sb.toString();
        }

        return key + buildNumberStr;
    }


}
